package org.gc.webapp.action.admin.rolegroup;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.gc.common.vo.security.RoleGroupVO;
import org.gc.common.vo.security.RoleVO;
import org.gc.service.security.RoleGroupService;
import org.gc.service.security.RoleService;
import org.gc.webapp.action.BaseAction;
import org.hibernate.exception.ConstraintViolationException;


public class RoleGroupAction extends BaseAction {

	private static final long serialVersionUID = -7930316282964050200L;

	private Long id;
	private RoleGroupVO roleGroup;

	private RoleGroupService roleGroupService;
	private RoleService roleService;
	private List<RoleGroupVO> rolegroupList;
	private List<RoleVO> roleList;
	private Set<String> roles;

	@Action("list")
	public String list() throws Exception {
		rolegroupList = roleGroupService.getAll();
		for(RoleGroupVO rg : rolegroupList){
			rg.getRoles().remove(roleService.getRole("ROLE_USER"));
			rg.getRoles().remove(roleService.getRole("ROLE_ADMIN"));
		}
		return super.execute();
	}

	@Action("edit")
	public String edit() {

		roleList = roleService.getAll();
		roleList.remove(roleService.getRole("ROLE_USER"));
		roleList.remove(roleService.getRole("ROLE_ADMIN"));

		if (id != null) {
			roleGroup = roleGroupService.get(id);
			
			Set<RoleVO> roleVOs = roleGroup.getRoles();
			roles= new HashSet<String>();
			for (RoleVO roleVO : roleVOs) {
				roles.add(roleVO.getName());
			}
		}
		return SUCCESS;
	}

	@Action(value = "save", results = {
			@Result(name = "success", type = "redirectAction", params = {
					"actionName", "list" }),
			@Result(name = "input", location = "edit.jsp") })
	public String save() {
		if (roleGroup != null) {
			try {
				roleGroupService.update(roleGroup, roles);
				saveMessage("角色组保存成功！");
			}catch(ConstraintViolationException e){
				addFieldError("roleGroup.name", "该角色组名称已经给使用！");
				return INPUT;
			}
		} else {
		}
		return SUCCESS;
	}

	@Action(value = "delete", results = { @Result(name = "success", type = "redirectAction", params = {
			"actionName", "list" }) })
	public String delete() {
		if (id != null) {
			try{
				roleGroupService.remove(id);
				saveMessage("删除成功！");
			}catch(Exception e){
				saveMessage(e.getMessage());
			}
		}
		return SUCCESS;
	}

	public void setRoleGroupService(RoleGroupService roleGroupService) {
		this.roleGroupService = roleGroupService;
	}

	public List<RoleGroupVO> getRolegroupList() {
		return rolegroupList;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public void setRoleGroup(RoleGroupVO roleGroup) {
		this.roleGroup = roleGroup;
	}

	public RoleGroupVO getRoleGroup() {
		return roleGroup;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleList(List<RoleVO> roleList) {
		this.roleList = roleList;
	}

	public List<RoleVO> getRoleList() {
		return roleList;
	}

	public void setRoles(Set<String> roles) {
		this.roles = roles;
	}

	public Set<String> getRoles() {
		return roles;
	}
}
